package cn.linstudy.controller;

import cn.linstudy.domain.Employee;
import cn.linstudy.qo.EmployeeQueryObject;
import cn.linstudy.qo.PageResult;
import cn.linstudy.result.ResponseResult;
import cn.linstudy.service.DepartmentService;
import cn.linstudy.service.EmployeeService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description
 * @Author XiaoLin
 * @Date 2021/3/2 20:35
 */
@Controller
@RequestMapping("/emp")
public class EmployeeController {

  @Autowired
  EmployeeService employeeService;

  @Autowired
  DepartmentService departmentService;


  @RequestMapping("listPage")
  public String ListPageDepartment(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
    PageResult<Employee> departmentPageResult = employeeService.listForPage(qo);
    ResponseResult responseResult = departmentService.listAll();
    model.addAttribute("departments",responseResult.getData());
    model.addAttribute("departmentPageResult",departmentPageResult);
    return "employee/list";
  }

  @RequestMapping("input")
  public String saveOrUpdate(Long id,Model model){
    if (id == null){
      model.addAttribute("departments",departmentService.listAll().getData());
      return "employee/input";
    }else {
      Employee employee = (Employee) employeeService.listOneById(id).getData();
      model.addAttribute("employee",employee);
      model.addAttribute("departments",departmentService.listAll().getData());
      return "employee/input";
    }
  }

  @RequestMapping("saveOrUpdate")
  public String saveOrUpdate(Model model,Employee employee){
    if (employee.getId() == null){
      employeeService.add(employee);
      return "redirect:/emp/listPage";
    }else {
      employeeService.update(employee);
      return "redirect:/emp/listPage";
    }
  }

  @RequestMapping("delete")
  public String delete(Long id){
    employeeService.delete(id);
    return "redirect:/emp/listPage";

  }

}
